import java.util.HashMap;
import java.util.Map;

public class Easy {
    public static void main(String[] args) throws Exception {
        int[] a = {9, 5, 7, 1, 6};

        System.out.println("twoSum: " + twoSum(a, 8));
    }

    //两数之和, 给定数组[2, 6, 3, 4, 8]和 Target = 9 因为6 + 3 = 9, 所以输出: [1, 2]
    static int[] twoSum(int[] arr, int target) {
        Map<Integer, Integer> intMap = new HashMap<>();
        for (int i = 0; i < arr.length; i++) {
            if(intMap.containsKey(target - arr[i])) {
                System.out.println("twoSum: " + intMap.get(target - arr[i]) + i);
                return new int[]{intMap.get(target - arr[i]), i};
            }
            intMap.put(arr[i], i);
        }

        return new int[0];
    }

    static int[] twoSum2(int[] arr, int target) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if(arr[i] + arr[j] == target) {
                    System.out.println("twoSum: " + i + j);
                    return new int[] {i, j};
                }
            }
        }

        return new int[0];
    }
}
